package com.acme.domain.account;

import java.io.Serializable;
import java.math.BigDecimal;

public abstract class AbstractAccount implements Account, Serializable {
    private static final long serialVersionUID = 1L;

    private final int id;
    private BigDecimal balance;

    public AbstractAccount(final int id, final BigDecimal amount) {
        this.balance = amount;
        this.id = id;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    protected void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public int getId() {
        return id;
    }

    protected void checkAmountIsPositive(BigDecimal amount) {
        if (amount.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("Amount can not be negative");
        }
    }

    /**
     * Maintains natural order by account number. Comparators may propose
     * another ways to sort.
     */
    public int compareTo(final Account account) {
        if (account.getId() < id) {
            return -1;
        } else if (account.getId() > id) {
            return 1;
        }
        return 0;
    }
}